/*
 * name: tip.js
 * version: v0.2.2
 * update: 修复ff取样式值bug
 * date: 2014-12-08
 */
define(function(require, exports, module) {
	require('./tip.css');
	var $ = require('jquery');
	var base = require('base');
	$.fn.tip = function(tip,config){
		var opts = {
				act: 'mouseenter',			// mouseenter | click
				place: 'left-center',		// [posi]-[posi]-[in or null]，前两项必须，表示位置，第三项表示从内部定位，可省
				title: false,				// title text | false
				hasarr: true,				// 有无箭头
				offset: 0,					// 提示框与元素间距，默认0
				callback: function($this){},
				ext: function($this){}
			},
			wrap = $('<div class="tip-box" id="tip-box">\
						<div class="tip-title" id="tip-title"></div>\
						<div class="tip-content" id="tip-content"></div>\
						<div class="tip-object" id="tip-object"></div>\
						<i class="tip-arr" id="tip-arr"></i><i class="tip-arr-cell" id="tip-arr-cell"></i>\
					</div>'),
			$this = $(this),
			place,
			show,
			offArr;
	
		$.extend(opts, config || {}, $this.data('config') || {});
		!$('#tip-box').length && $('body').append(wrap);
		place = opts.place.split('-');
		offArr = (opts.hasarr ? parseInt(base.getStyle($('#tip-arr').get(0),'border-top-width')) : 0) + opts.offset;
		show = function(){
			var	_tipLeft,
				_tipTop,
				_offX,
				_offY
				_classCatch = '';
				
			$('#tip-box').removeClass('tip-withContent tip-withObject tip-withTitle tip-noTitle tip-left tip-top tip-right tip-bottom');
			$this.addClass('showTip');
			if(typeof(tip)==='string'){
				_classCatch += 'tip-withContent ';
				$('#tip-object').empty();
				$('#tip-content').html(tip);
			}else if(typeof(tip)==='object'){
				_classCatch += 'tip-withObject ';
				$('#tip-content').empty();
				$('#tip-object').html($(tip));
			}
			
			if(opts.title){
				_classCatch += 'tip-withTitle ';
				$('#tip-title').text(opts.title);
			}else{
				_classCatch += 'tip-noTitle ';
				$('#tip-title').empty();
			};
			
			_offX = $('#tip-box').outerWidth()>$this.outerWidth() ? -Math.abs($this.outerWidth()-$('#tip-box').outerWidth())/2 : Math.abs($this.outerWidth()-$('#tip-box').outerWidth())/2,
			_offY = $('#tip-box').outerHeight()>$this.outerHeight() ? -Math.abs($this.outerHeight()-$('#tip-box').outerHeight())/2 : Math.abs($this.outerHeight()-$('#tip-box').outerHeight())/2;
			var _getLeft = function(){
				switch (place[1]){
					case 'center':
						_tipLeft = $this.offset().left + _offX;
						break;
					case 'left':
						_tipLeft = $this.offset().left;
						break;
					case 'right':
						_tipLeft = $this.offset().left +$this.outerWidth()-$('#tip-box').outerWidth();
						break;
					default:
					place[2] = place[1];
				}
			},
			_getTop = function(){
				switch (place[1]){
					case 'center':
						_tipTop = $this.offset().top + _offY;
						break;
					case 'top':
						_tipTop = $this.offset().top;
						break;
					case 'bottom':
						_tipTop = $this.offset().top + $this.outerHeight() - $('#tip-box').outerHeight();
						break;
					default:
					place[2] = place[1];
				}
			};

			switch (place[0]){
				case 'top':	
					_getLeft();
					_tipTop = $this.offset().top - $('#tip-box').outerHeight() - offArr;
					if(place[2]==='in'){
						_tipTop = _tipTop + $this.outerHeight();
					}
					break;
				case 'bottom':
					_getLeft();
					_tipTop = $this.offset().top + offArr;
					if(place[2]!=='in'){
						_tipTop = _tipTop + $this.outerHeight();
					}
					break;
				case 'right':
					_getTop();
					_tipLeft = $this.offset().left + offArr;
					if(place[2]!=='in'){
						_tipLeft = _tipLeft + $this.outerWidth();
					}
					break;
				default:
					// left
					_getTop();
					_tipLeft = $this.offset().left - $('#tip-box').outerWidth() - offArr;
					if(place[2]==='in'){
						_tipLeft = _tipLeft + $this.outerWidth();
					}
			}
			if(opts.hasarr){
				_classCatch += 'tip-'+place[0]
			};

			$('#tip-box').addClass(_classCatch).css('left',_tipLeft).css('top',_tipTop).show();
			
			opts.callback && opts.callback($this)
		};
				
		if(opts.act==='mouseenter'){
			$this.on('mouseenter',function(){
				show();
				if(place[1]==='in'){
					$('#tip-box').on('mouseleave',function(){
						$('#tip-box').hide();
						$this.removeClass('showTip');		
					})
				}
			});
			if(place[1]!=='in'){
				$this.on('mouseleave',function(){
					$('#tip-box').hide();
					$this.removeClass('showTip');		
				})
			};
			
		}else if(opts.act==='click'){
			$('body').on('click',function(e){
				if($this.is(e.target)){
					e.preventDefault();
					$('#tip-box').unbind();
					setTimeout(function(){
						show();
					})
				}else{
					$('#tip-box').hide();
					$this.removeClass('showTip');
				}
			})
		}
				
		opts.ext && opts.ext($this);
		
		return $this;
	}

})